// pages/myself/authen.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zncfiles: [],
    zncUpload: true,
    countries: ["教练", "陪练", "销售员","技师"],
    countryIndex: 0,
    showsavebtn:false,
    disapply:false,
    isapply:0,
    hasDesc: false,
    rolename:'',
    steps: [
      {
        current: true,
        done: true,
        text: '审批中',
        desc: '描述一'
      },
      {
        current: false,
        done: false,
        text: '审批通过',
        desc: '描述二'
      }
    ] 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var uid = wx.getStorageSync('userId');
    wx.request({
      url: app.globalData.hostUrl + 'member/MyImages',
      data: {
        uid: uid,
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 200) {
          if (res.data.result.roleauth == 1) {
            that.setData({
              disapply: true,
              rolename: res.data.result.user.role
            });
          }

          var flag = 1;
          if (res.data.result.user.realname == '') {
            flag = 0;
          }
          if (res.data.result.user.phone == '') {
            flag = 0;
          }
          if (res.data.result.user.bankno == '') {
            flag = 0;
          }
          if (res.data.result.user.bankaddr == '') {
            flag = 0;
          }
          if (flag == 0) {
            that.setData({
              showsavebtn: true
            });
            wx.showModal({
              title: '提示',
              content: '请先在【个人中心】里完善真实姓名，电话，银行账户信息！',
              showCancel: true,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '../myself/info',
                  })
                }
                if (res.cancel) {
                  wx.navigateTo({
                    url: '../myself/myself',
                  })
                }
              }
            })
            return false;
          }
          var applay = res.data.result.user.isapply;
          that.setData({
            zncfiles: res.data.result.imgfiles == null ? [] : res.data.result.imgfiles,
            showsavebtn: applay == 0 ? false : (applay == -1 ? false : true),
            isapply: applay == -1 ? -1 : 0
          })
          if (applay == 2) {
            var oldsteps = that.data.steps;
            oldsteps[0].current = false;
            oldsteps[1].current = true;
            oldsteps[1].done = true;
            that.setData({
              steps: oldsteps
            })
          }
          if (that.data.zncfiles.length >= 2) {
            that.setData({
              zncUpload: false
            });
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  bindCountryChange: function (e) {
    this.setData({
      countryIndex: e.detail.value
    })
  },
  choosezncImg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 2,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
       // console.log(tempFilePaths);
        that.setData({
          zncfiles: that.data.zncfiles.concat(res.tempFilePaths)
        });
        //上传图片
        app.uploadimg({
          url: app.globalData.hostUrl + 'member/applyUpload', //这里是你图片上传的接口
          path: tempFilePaths, //这里是选取的图片的地址数组
          imgtype: 'applayimg'
        });
        if (that.data.zncfiles.length >1) {
          that.setData({
            zncUpload: false
          });
        }
      }
    })
  },
  // 删除图片
  deleteImg: function (e) {
    if (this.data.mid > 0) {
      return false;
    }
    var that = this;
    var name = e.currentTarget.dataset.name;
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    var imgfiles = this.data.zncfiles;
   
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          var str = imgfiles[index];
          var imgName = str.substring(str.lastIndexOf("/") + 1);
          //console.log(imgName);
          wx.request({
            url: app.globalData.hostUrl + 'member/Delimage', //删除上传图片
            data: {
              imgname: imgName
            },
            success: function (res) {
//              console.log(res);
              if (res.data.code == 200) {
                imgfiles.splice(index, 1);
                that.setData({
                  zncfiles: imgfiles,
                  zncUpload: true
                });
              }
            }
          })

        } else if (res.cancel) {
          console.log('点击取消删除');
          return false;
        }

      }
    })
  },
  previewImage: function (e) {
    var imgfiles = null;
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    imgfiles = this.data.zncfiles;

    var imgurl = [imgfiles[index]];
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: imgurl // 需要预览的图片http链接列表
    })
  },
  formSubmit: function (e) {
    var that=this;
    var getdata = {
      uid: wx.getStorageSync('userId'),
      rolename: this.data.countries[this.data.countryIndex],
    }
    wx.request({
      url: app.globalData.hostUrl + 'member/applay',
      data: getdata,
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            showsavebtn:true
          });
          wx.showModal({
            title: '提示',
            content: '您的申请已提交，请等待审批!',
            showCancel:false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../myself/myself',
                })
              }
            }
          })
        }
        else{
          wx.showToast({
            title: '申请提交失败!',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})